package cn.sh.yhk.steam;

import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {

    public static void generateTest() {
        // 生成自己的随机数流
        Random random = new Random();
        Stream<Integer> generateRandom = Stream.generate(random::nextInt);
        generateRandom.limit(5).forEach(System.out::println);

        // 生成自己的 UUID 流
        Stream<UUID> generate = Stream.generate(UUID::randomUUID);
        generate.limit(5).forEach(System.out::println);
    }

    public static void main(String[] args) {
        Random random = new Random();
        Stream<Integer> generateRandom = Stream.generate(random::nextInt);
        List<Integer> numberList = generateRandom.limit(10000000).collect(Collectors.toList());

        // 串行 - 把一千万个随机数，每个随机数 * 2 ，然后求和
        long start = System.currentTimeMillis();
        int sum = numberList.stream().map(number -> number * 2).mapToInt(x -> x).sum();
        long end = System.currentTimeMillis();
        System.out.println("串行耗时：" + (end - start) + "ms，和是:" + sum);

        // 并行 - 把一千万个随机数，每个随机数 * 2 ，然后求和
        start = System.currentTimeMillis();
        sum = numberList.parallelStream().map(number -> number * 2).mapToInt(x -> x).sum();
        end = System.currentTimeMillis();
        System.out.println("并行耗时：" + (end - start) + "ms，和是:" + sum);


//
//        List<String> nameList = Arrays.asList("Darcy", "Chris", "Linda", "Sid", "Kim", "Jack", "Poul", "Peter");
//        String[] nameArr = {"Darcy", "Chris", "Linda", "Sid", "Kim", "Jack", "Poul", "Peter"};
//        Map<String, List<String>> stringListMap = nameList.stream().collect(Collectors.groupingBy(name->name.substring(0,1)));
//        stringListMap.forEach((k, v) -> {
//            System.out.println("字母" + k + "有：" + v);
//        });


//        nameList.stream()
//                .filter(name -> name.length() == 4)
//                .map(name -> "this is " + name)
//                .map(name -> name.toUpperCase())
//                .forEach(name -> System.out.println(name));
    }
}
